package views.components;

import java.util.List;

import utils.AppUtils;
import app.ApplicationManager;
import app.structure.Arg;
import app.structure.MenuFrm;
import app.structure.MenuProc;
import app.structure.MenuStruct;

import com.vaadin.annotations.Theme;
import com.vaadin.event.MouseEvents.ClickEvent;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;

import config.Parameters;

@SuppressWarnings("serial")
@Theme("Theme02")
public class OptLink extends CustomComponent {
	private final HorizontalLayout hL = new HorizontalLayout();
	private final Image img;
	private Label lblA;
	private String id;
	private String typ;
	// Type: M->Menu Folder,
	// F-> Class Form (with parameters),
	// P-> Procedure/Program (with parameters and form)
	// R-> Report (with parameters and form),
	// T-> Transaction (with parameters and form)
	private String clazz; // Name of the class
	private String method_view; // Name of the view (F) or method (P,R,T)
	private String formYN; // Show form Y or NOT in (P,R,T)
	private List<Arg> args; // arguments to pass

	public OptLink(MenuStruct mnuStr) {
		
		this.typ = mnuStr.getTyp();
		this.id = mnuStr.getId();
		img = getImage(typ);

		this.getActionInfo(mnuStr);

		lblA = new Label(mnuStr.getName());

		hL.addComponent(img);
		hL.addComponent(lblA);

		setCompositionRoot(hL);
	}

	private Image getImage(String type) {

		Image im;
		// 1. Form
		if (type.equalsIgnoreCase("F")) {
			im = new Image("", new ThemeResource(Parameters.ICO_COMMON_PATH
					+ "form.ico"));

		}
		// 2. Procedure
		else if (type.equalsIgnoreCase("P")) {
			im = new Image("", new ThemeResource(Parameters.ICO_COMMON_PATH
					+ "procedure.ico"));
		}
		// 3. Report
		else if (type.equalsIgnoreCase("R")) {
			im = new Image("", new ThemeResource(Parameters.ICO_COMMON_PATH
					+ "report.ico"));
		}
		// 4. Transaction
		else if (type.equalsIgnoreCase("T")) {
			im = new Image("", new ThemeResource(Parameters.ICO_COMMON_PATH
					+ "transaction.ico"));
		}
		// 5. Error in type
		else {
			im = new Image("", new ThemeResource(Parameters.ICO_COMMON_PATH
					+ "question.ico"));
		}
		return im;
	}

	private void getActionInfo(MenuStruct mnuStr) {
		ApplicationManager appMngr = AppUtils.getApplicationManager();
		if (typ.equalsIgnoreCase("F")) {
			MenuFrm mnuFrm = appMngr.getForm(mnuStr.getId());
			clazz = mnuFrm.getClazz();
			method_view = mnuFrm.getView();
			formYN = "Y";
			args = mnuFrm.getArgs();
		} else {
			MenuProc mnuProc = appMngr.getProc(mnuStr.getId());
			clazz = mnuProc.getClazz();
			method_view = mnuProc.getMethod();
			formYN = mnuProc.getForm();
			args = mnuProc.getArgs();
		}
	}

	public void buttonClick(ClickEvent event) {
		Notification.show("You clickked " + id + " " + clazz + " "
				+ method_view + " " + formYN);
	}

}
